#pragma once

#include <assert.h>

namespace Simulation
{
    template <class T>
    class vector
    {
    public:
        typedef T *iterator;
        typedef const T *const_iterator;
        vector()
            : _start(nullptr), _finish(nullptr), _endofstorage(nullptr)
        {
        }

        vector(const vector<T> &v)
        {
            _start = new T[v.capacity()];
            _finish = _start + v.size();
            _endofstorage = _start + v.capacity();

            // memcpy(_start, v._start, sizeof(T) * v.size());
            for (size_t i = 0; i < size(); ++i)
            {
                _start[i] = v._start[i];
            }
        }
        ~vector()
        {
            delete[] _start;
            _start = _finish = _endofstorage = nullptr;
        }
        void reserve(size_t n)
        {
            int sz = size();
            if (n > capacity())
            {
                T *tmp = new T[n];
                if (_start)
                {
                    // memcpy(tmp, _start, sizeof(T) * sz);// 浅拷贝，对于自定义类型会出错，string类
                    //  赋值 对于string类就是赋值重载，也是深拷贝
                    for (size_t i = 0; i < size(); ++i)
                    {
                        tmp[i] = _start[i];
                    }
                }
                delete[] _start;
                _start = tmp;
                _finish = _start + sz;
                _endofstorage = _start + n;
            }
        }
        void push_back(T x)
        {
            insert(_finish, x);
        }
        void pop_back()
        {
            /*assert(_finish > _start);
            --_finish;*/
            erase(_finish - 1);
        }
        void insert(iterator pos, const T &x)
        {
            assert(pos <= end());

            if (_finish == _endofstorage)
            {
                // 算出原先差距，以免迭代器失效带来的一些问题
                int gap = end() - pos;
                size_t newcapacity = capacity() == 0 ? 2 : 2 * capacity();
                reserve(newcapacity);
                pos = end() + gap;
            }

            iterator end = _finish;
            while (pos < end)
            {
                *end = *(end - 1);
                --end;
            }
            *pos = x;
            ++_finish;
        }
        iterator erase(iterator pos)
        {
            assert(pos < _finish && pos >= _start);
            iterator start = pos;
            while (start + 1 < _finish)
            {
                *start = *(start + 1);
                ++start;
            }
            --_finish;

            return pos;
        }
        T &operator[](size_t i) const
        {
            assert(i < size());
            return _start[i];
        }
        void resize(size_t n, const T &val = T()) // T()是T类型的缺省值，具体是什么也不清楚
        {
            size_t sz = size();
            if (n < sz)
            {
                _finish = _start + n;
            }
            else
            {
                if (n > capacity())
                {
                    reserve(n);
                }
                iterator end = _finish;
                while (end < _start + n)
                {
                    *end = val;
                    ++end;
                }
                _finish = _start + n;
            }
        }
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const_iterator begin() const
        {
            return _start;
        }
        const_iterator end() const
        {
            return _finish;
        }
        size_t size() const
        {
            return _finish - _start;
        }
        size_t capacity() const
        {
            return _endofstorage - _start;
        }
        bool empty()
        {
            return size() == 0;
        }
        void clear()
        {
            _finish = _start;
        }
        T back()
        {
            if (_finish == _start)
                exit(-1);
            return *(_finish - 1);
        }

    private:
        iterator _start;        // 起始位置的指针
        iterator _finish;       // _start+size
        iterator _endofstorage; // _start+capacity
    };
}
